package com.doubi.service;

import com.alibaba.fastjson.JSONObject;
import com.doubi.constants.GenConstants;
import com.doubi.utils.string.StringUtils;
import org.apache.commons.io.IOUtils;
import com.doubi.constants.SystemConstants;
import com.doubi.mapper.GenTableColumnMapper;
import com.doubi.mapper.GenTableMapper;
import com.doubi.model.sys.GenTable;
import com.doubi.model.sys.GenTableColumn;
import com.doubi.util.GenUtils;
import com.doubi.util.VelocityInitializer;
import com.doubi.util.VelocityUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 业务 服务层实现
 */
@Service
public class GenTableService {

    @Autowired
    private GenTableMapper genTableMapper;

    @Autowired
    private GenTableColumnMapper genTableColumnMapper;


    /**
     * 查询据库列表
     * @return 数据库表集合
     */
    public List<GenTable> selectDbTableList()
    {
        return genTableMapper.selectDbTableList();
    }
    /**
     * 生成代码
     * 
     * @param tableName 表名称
     * @return 数据
     */
    public byte[] generatorCode(String tableName,String moduleName)
    {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        generatorCode(tableName,moduleName, zip);
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }


    /**
     * 查询表信息并生成代码
     */
    private void generatorCode(String tableName,String moduleName, ZipOutputStream zip)
    {
        // 查询表信息
        GenTable table = genTableMapper.selectTableByName(tableName);
        table.setClassName(GenUtils.convertClassName(tableName));
        table.setModuleName(moduleName);
        // 查询列信息
        List<GenTableColumn> columns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
        table.setColumns(columns);
        for (GenTableColumn column : columns){
            GenUtils.initColumnField(column, table);
        }
        setPkColumn(table, columns);

        VelocityInitializer.initVelocity();

        VelocityContext context = VelocityUtils.prepareContext(table);

        // 获取模板列表
        List<String> templates = VelocityUtils.getTemplateList();
        for (String template : templates)
        {
            // 渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, SystemConstants.UTF8);
            tpl.merge(context, sw);
            try
            {
                // 添加到zip
                zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
                IOUtils.write(sw.toString(), zip, SystemConstants.UTF8);
                IOUtils.closeQuietly(sw);
                zip.flush();
                zip.closeEntry();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置主键列信息
     * 
     * @param table 业务表信息
     * @param columns 业务字段列表
     */
    public void setPkColumn(GenTable table, List<GenTableColumn> columns)
    {
        for (GenTableColumn column : columns)
        {
            if (column.isPk())
            {
                table.setPkColumn(column);
                break;
            }
        }
        if (StringUtils.isNull(table.getPkColumn()))
        {
            table.setPkColumn(columns.get(0));
        }
    }
}